package edu.nf.ch02.controller;

import edu.nf.ch02.common.MinioOptions;
import edu.nf.ch02.vo.ResultVO;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @ClassName UploadController
 * @Description TODO
 * @Author Kx
 * @Date 2025/9/4 22:53
 * @Version 1.0
 */
@RestController
public class UploadController extends BaseController{
    private MinioOptions minioOptions;

    public UploadController(MinioOptions minioOptions) {
        this.minioOptions = minioOptions;
    }

    /**
     * @author: kx
     * @date: 2025/9/4 22:57
     * @param path : 文件保存路径
     * @param files : 文件数组
     * @return edu.nf.ch02.vo.ResultVO
     * @description: 文件上传
    */
    @PostMapping("/upload/{path}")
    public ResultVO upload(@PathVariable("path") String path, @RequestParam("file") MultipartFile[] files) throws IOException {
        for (MultipartFile file : files) {
            // 获取文件名
            String fileName = file.getOriginalFilename();
            // 获取输入流
            InputStream input = file.getInputStream();
            // 获取文件类型
            String contentType = file.getContentType();
            // 获取文件大小
            long size = file.getSize();
            // 上传文件
            minioOptions.upload(path + "/" + fileName, contentType, size, input);
        }
        return success();
    }
}
